package indi.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import indi.entity.Manager;
import indi.service.UserService;

/*
 * 添加/删除/查询/更新 书本/管理员/标题
 * 
 */
public class LoginFrame extends JFrame{

	private JPanel contentPane;
	private JTextField txtUserName;
	private JPasswordField fldPassWord;

	public LoginFrame() {
		super("登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100, 450, 300);//100 100 450 300// 25, 225, 425, 25
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 设置边界
		contentPane.setLayout(new BorderLayout(0,0));
		setContentPane(contentPane);
		setVisible(true);
		
		// 总面板
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		// 用户名
		JLabel lblUserName = new JLabel("用户名：");
		lblUserName.setBounds(new Rectangle(52, 51, 60, 18));
		lblUserName.setForeground(new Color(0,0,0));// 灰色？
		panel.add(lblUserName);
		
		txtUserName = new JTextField();
		txtUserName.setBounds(126, 48, 216, 24);
		panel.add(txtUserName);
		txtUserName.setColumns(10);//?
		
		// 密码
		JLabel lblPwd = new JLabel("密码：");
		lblPwd.setBounds(68, 105, 72, 18);
		panel.add(lblPwd);
		
		fldPassWord = new JPasswordField();
		fldPassWord.setBounds(126, 102, 216, 24);
		panel.add(fldPassWord);
		fldPassWord.setColumns(10);
		// 登录按钮
		JButton btOk = new JButton("登录");
		btOk.addActionListener(new LoginListener());
		btOk.setBounds(90, 172, 72, 27);
		panel.add(btOk);
		
		//重置按钮
		JButton btRt = new JButton("重置");
		btRt.setBounds(176, 172, 72, 27);
		btRt.addActionListener(new LoginListener());
		panel.add(btRt);
		
		// 注册
		JButton btRg = new JButton("注册");
		btRg.setBounds(265, 172, 72, 27);
		btRg.addActionListener(new RegistListener());
		panel.add(btRg);
		
	}
	// 监听类，负责处理确认按钮的业务逻辑
	private class LoginListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			String userName = txtUserName.getText().trim();
			String pwd = new String(fldPassWord.getPassword());
			UserService service = new UserService();
			
			try {
				Manager currentManager = service.findUserByName(userName);
				if(currentManager == null) {
					JOptionPane.showMessageDialog(null, "账号错误", "错误提示", JOptionPane.ERROR_MESSAGE);
				}
				else if(currentManager.getPwd().equals(pwd)){
					JOptionPane.showMessageDialog(null, "登录成功", "成功提示", JOptionPane.CANCEL_OPTION);
					LoginFrame.this.setVisible(false);
					new MainFrame(currentManager);
				}
				else {
					JOptionPane.showMessageDialog(null, "密码错误", "错误提示", JOptionPane.ERROR);
				}
			}catch(Exception ex) {
				e.paramString();
			}
		}
	}
	
	// 监听器，负责处理重置按钮的业务逻辑
	private class ResetListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// 设置用户名密码为“”
			txtUserName.setText("");
			fldPassWord.setText("");
		}
	}
	
	// 监听类，负责处理注册按钮
	private class RegistListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			new RegistFrame();
		}
	}
	
	public static void main(String[] args) {
		new LoginFrame();
	}
}
